package com.hmjk.health.views;

import com.hmjk.health.R;

import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class BeautyButton extends LinearLayout{
	private final String TAG=BeautyButton.class.getSimpleName();
	private RelativeLayout imgview;
	private ImageView icon;
	private TextView msg;
	private TextView txt;
	private ColorStateList mTextColor = null;
	private int mTextDefaultColor = 0;
	private Drawable mImageSet = null;
	private boolean focused = false;
	private boolean pressed = false;
	private Context mContext = null;
	private int msgs = 0;
	private String mTxtString = null;
	
	private void logd(String info){
	//	Log.d(TAG, info);
	}
	
	public BeautyButton(Context context) {
		super(context);
		mContext = context;
	}

	public BeautyButton(Context context, AttributeSet attrs) {
		super(context, attrs);
		mContext = context;
	}
	
	public BeautyButton(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		mContext = context;
	}
	
	public void setImageSet(Drawable d){
		mImageSet = d; 
	}
	
	public void setColorList(ColorStateList color){
		mTextColor = color;
		mTextDefaultColor = mTextColor.getDefaultColor();
	}
	
	public void setColor(int color){
		mTextColor = null;
		mTextDefaultColor = color;
	}
	
	public void setText(int txtres){
		mTxtString = mContext.getResources().getString(txtres);
		if(txt != null)
			txt.setText(mTxtString);
	}
	
	public void setMsgCount(int count){
		msgs = count;
		updateViews();
	}
	
	@Override
	public void onAttachedToWindow(){
		super.onAttachedToWindow();
		init();
	}
	
	private void init(){
		logd("init(" + this.getId() + ")");
		if(getChildCount()!=2){
			throw new IllegalStateException("ImageView and TextView must be added");
		}
		imgview = (RelativeLayout)getChildAt(0);
		icon = (ImageView) imgview.findViewById(R.id.icon);
		msg = (TextView) imgview.findViewById(R.id.msg);
		txt = (TextView)getChildAt(1);
		if(mTxtString != null)
			txt.setText(mTxtString);
		updateViews();
	}
	
	
	private void updateViews(){
		logd("updateViews(" + this.getId() + ")");
		if((icon == null) || (txt == null) || (msg == null)){
			logd("updateViews(" + this.getId() + ") icon ="+icon + " txt="+txt + " msg="+msg);
			return;
		}
		
		int color = mTextDefaultColor;
		Drawable d = null;
		
		logd("updateViews(" + this.getId() + ") focused ="+focused + " pressed="+pressed);
		if(focused || pressed){
			if(mTextColor != null){
				color = mTextColor.getColorForState(View.PRESSED_SELECTED_STATE_SET, color);
			}
			if(mImageSet != null){
				mImageSet.setState(View.PRESSED_SELECTED_STATE_SET);
				d = mImageSet.getCurrent();
			}
		}else{
			if(mImageSet != null){
				mImageSet.setState(View.EMPTY_STATE_SET);
				d = mImageSet.getCurrent();
			}
		}
		
		txt.setTextColor(color);
		if(d != null)
			icon.setImageDrawable(d);
		
		if(msgs > 0){
			if(msgs > 99){
				msg.setText("99+");
			}else{
				msg.setText(String.valueOf(msgs));
			}
			msg.setVisibility(View.VISIBLE);
		}else{
			msg.setVisibility(View.INVISIBLE);
		}
	}
	
	public void setFocused(boolean set){
		logd("setFocused(" + this.getId() + ") set = "+set);
		focused = set;
		updateViews();
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent e){
		if(MotionEvent.ACTION_DOWN == e.getAction()){
			pressed = true;
			updateViews();
			return super.onTouchEvent(e);
		}else if(MotionEvent.ACTION_UP == e.getAction()){
			boolean ret = super.onTouchEvent(e);
			pressed = false;
			updateViews();
			return ret;
		}
		
		return super.onTouchEvent(e);
	}
}
